<template>
  <div v-if="is_info">
    <el-switch :model-value="this_value" :size="size" inline-prompt :inactive-text="options[0]['content']" :active-text="options[1]['content']"></el-switch>
  </div>
  <div v-else class="form_item-radio_switch">
    <input type="hidden" :name="name" :value="value">
    <el-switch :model-value="this_value" :size="size" inline-prompt :inactive-text="options[0]['content']" :active-text="options[1]['content']" @change="update_value"></el-switch>
  </div>
</template>

<script>
import {decode_config} from "@/js/s94_tool.js";

export default {
  _title_: '开关',
  _can_config_: true,
  _can_form_param_public_: true,
  _can_form_param_: true,
  name: "radio_switch",
  props: ['name', 'value', 'config', 'is_info', 'size', 'is_list'],
  emits: ['update:value', 'change'],
  data(){
    return {
      need_display: true,
      this_value: false,
    }
  },
  methods: {
    update_value(v){
      let row = this.options[v?1:0];
      v = row.value;
      // this.need_display = false;
      // setTimeout(()=>{this.need_display = true;},5);
      this.$emit('update:value', v);
      this.$emit('change', v);
    },
  },
  watch: {
    value(){
      if (!this.need_display) return;
      this.this_value = (this.value == this.options[1]['value']);
    }
  },
  computed: {
    options(){
      let config = decode_config(this.config || ['否','是']);
      if (config.length != 2) config = decode_config(['否','是']);
      return config;
    }
  },
  mounted() {
    this.this_value = (this.value == this.options[1]['value']);
  }
}
</script>

<style scoped>

</style>